// pages/index/business/search/index.js
// import { selectData } from "../../../../utils/data/tabbar";
import { serviceListPage, getServiceType } from  "../../../../utils/api/module/cyfw";

Page({
    data: {
        isLineData: 0,
        serviceData: [], // list
        searchType: 1, // 筛选弹窗控制
        selectData: [], // 搜索选项
        page: 1,
        typeid: '', // 筛选id
        keywords: ''
    },
    onLoad: function (options) {
        this.data.typeid = !options.id  ? '' : options.id;
        this.setData({ typeid: this.data.typeid })
        this.getData();
        this.getServiceType(); // 服务选择
    },
    getData() {
        serviceListPage({
            page: this.data.page,
            limit: 10,
            typeid: this.data.typeid,
            keywords: this.data.keywords
        }).then(res => {
            if(this.data.page == 1) this.setData({ serviceData: [] });
            if(res.data.data) {
                this.setData({
                    ['serviceData[' + (this.data.page - 1) + ']']: res.data.data.rows,
                    isLineData: this.data.page !== 1 && res.data.data.length < 10 ? 2 : 0
                });
                return false;
            };
            this.setData({
                ['policyList[' + (this.data.page - 1) + ']']: [],
                isLineData: 0
            });
            wx.showToast({title: res.data.message, icon: "none" });
        });
    },
    getInputVal: function() {
        this.data.page = 1;
        this.getData();
    },
    input: function(e) {
        this.setData({
            keywords: e.detail.value 
        })
    },
    // 筛选数据
    getServiceType: function() {
        getServiceType({
            parent: 0,
            type: 'allchild'
        }).then(res => {
            this.data.selectData = res.data.data;
            this.setData({ selectData: this.data.selectData })
        });
    },
    // 筛选弹窗
    bindSearch: function() {
        this.setData({ searchType: this.data.searchType === 1 ? 2 : 1 });
    },
    // 筛选列表点击
    bindList: function(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({ typeid: Number(index) });
    },
    // 重置
    replacement: function() {
        this.setData({ typeid: null});
    },
    // 确认
    bindSave: function() {
        this.catchSearch(); // 关闭弹窗
        this.data.page = 1;
        this.getData();
    },
    // 关闭弹窗
    catchSearch: function() {
        this.setData({ searchType: 1 });
    },
    onReachBottom: function() {
        let data = this.data.serviceData;
        if(data[(this.data.page - 1)].length < 10) {
            this.setData({ isLineData: 2 });
            return false;
        };
        this.setData({ isLineData: 1 });
        this.data.page += 1;
        this.getData();
    }
})